/*
* popupwnd.cpp - This file is the part of the Miscellaneous Files.
* Copyright (C) 2010 Dean Lee <deanlee3@gmail.com>, http://www.deanlee.cn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
* Acknowledgements:
*   <> Many thanks to all of you, who have encouraged me to update my articles
*     and code, and who sent in bug reports and fixes.
* 
*/
#include "popupwnd.h"
#include <QTimer>
#include <QApplication>
#include <QDesktopWidget>
#include <QLabel>
#include <QMouseEvent>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QToolButton>
#include <QPixmap>
#include "configdlg.h"
#include "blog.h"

// Holds a list of Psi Popups.
static QList<PopupWnd*> *wpPopupList = 0;


PopupWnd::PopupWnd(const Blog& blog) : QFrame( 0, Qt::ToolTip | Qt::WindowStaysOnTopHint)
{
	m_blog = blog;
	QWidget::setAttribute(Qt::WA_DeleteOnClose);
	setWindowModality(Qt::NonModal);
	m_hideTimer = new QTimer(this);
	connect(m_hideTimer, SIGNAL(timeout()), this, SLOT(hide()));

	QVBoxLayout* vbMain = new QVBoxLayout(this);
	QLabel* lbIcon = new QLabel(this);
	lbIcon->setPixmap(QPixmap(":/icons/comment.png"));
	QLabel* lbTitle = new QLabel(this);
	lbTitle->setObjectName("PopupTitle");
	QToolButton* tbClose = new QToolButton(this);
	tbClose->setText("x");
	tbClose->setToolTip(tr("Close"));
	connect(tbClose, SIGNAL(clicked()), SLOT(hide()));

	
	QHBoxLayout* hl = new QHBoxLayout(this);
	hl->addWidget(lbIcon);
	hl->addWidget(lbTitle);
	//lbTitle->setText(tr("Wordpress comments notifier"));
	lbTitle->setText(m_blog.name());
	hl->addStretch(1);
	hl->addWidget(tbClose);
	vbMain->addLayout(hl);

	m_message = new QLabel(this);
	m_message->setObjectName("PopupContent");
	m_message->setToolTip(tr("Click to open main window"));
	m_message->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	vbMain->addWidget(m_message);

	if ( !wpPopupList )
		wpPopupList = new QList<PopupWnd *>();
	
	m_prevPopups = *wpPopupList;
	/*PopupWnd *lastPopup = 0;
	if ( wpPopupList->count() && wpPopupList->last() )
		lastPopup = wpPopupList->last();*/
	wpPopupList->append(this);

	
}

void PopupWnd::setMessage(const QString& message)
{
	m_message->setText(message);
}


PopupWnd::~PopupWnd()
{
	if ( wpPopupList )
		wpPopupList->removeAll(this);
}

QPoint PopupWnd::position()
{
	QRect geom = qApp->desktop()->availableGeometry(this);
	QPoint destination(geom.x() + geom.width(), geom.y() + geom.height()); // in which corner popup should appear

	//if ( destination.y() > (qApp->desktop()->screenGeometry().height()/2) )
	//	popupLayout = Private::BottomToTop;
	//else
	//	popupLayout = Private::TopToBottom;

	if ( (destination.x() + width()) > (geom.x() + geom.width()) )
		destination.setX( geom.x() + geom.width() - width() );

	if ( destination.x() < 0 )
		destination.setX( 0 );

	if ( (destination.y() + height()) > (geom.y() + geom.height()) )
		destination.setY( geom.y() + geom.height() - height() );

	if ( destination.y() < 0 )
		destination.setY( 0 );

	foreach ( PopupWnd *p, m_prevPopups )
		destination.setY( destination.y() - p->height() );

	return destination;
}

void PopupWnd::mouseReleaseEvent(QMouseEvent *e)
{
	if (!isVisible())
		return;

	emit clicked((int)e->button());
	hide();
}

void PopupWnd::show()
{
	if ( size() != sizeHint() )
		resize( sizeHint() ); // minimumSizeHint()

	// QLabels with rich contents don't propagate mouse clicks
	QList<QLabel *> labels = findChildren<QLabel *>();
	foreach(QLabel *label, labels)
		label->installEventFilter(this);

	// position popup
	move (position() );

	// display popup
	restartHideTimer();
	QFrame::show();
}

void PopupWnd::restartHideTimer()
{
	m_hideTimer->start(300 * 1000); // 30 seconds
}

bool PopupWnd::eventFilter(QObject * watched, QEvent * e)
{
	if (e->type() == QEvent::MouseButtonRelease)
		mouseReleaseEvent((QMouseEvent *)e);
	return QFrame::eventFilter(watched, e);
}

void PopupWnd::hideEvent(QHideEvent *e)
{
	m_hideTimer->stop();
	deleteLater();

	QFrame::hideEvent(e);
}

